from pythonwork.Art.PicCarwler import Send
from pythonwork.Art.ThreadDownLoad import ThreadDownLoad
from bs4 import BeautifulSoup

def parseList(response):
    print("首页解析开始")
    soup = BeautifulSoup(response)
    a_tag = soup.select(".thumb a")
    set_url = "http://www.qiaotu.org"
    pic_list = []
    for info in a_tag:
        href = set_url + info["href"]
        pic_list.append(href)
        print(href)
    print("首页解析结束->返回首页链接")
    return pic_list

def parseDetail(response):
    print("详情页解析开始")
    soup = BeautifulSoup(response)
    a_tag = soup.select(".loop-pagination a")
    set_url = "http://www.qiaotu.org"
    pic_list = []
    for info in range(len(a_tag)-1):
        href = set_url + a_tag[info]["href"]
        pic_list.append(href)
        print("详情页链接"+href)
    print("详情页解析结束->返回详情页链接")
    return pic_list

def getPic(response):
    print("详情页解析开始")
    soup = BeautifulSoup(response)
    a_tag = soup.select(".centralnav img")
    set_url = "http://www.qiaotu.org"
    pic_list = []
    print(a_tag)
    for info in a_tag:
        href = info["src"]
        pic_list.append(href)
        print("图片链接" + href)
    print("图片链接->图片链接结束")
    return pic_list


if __name__ == "__main__":
    url = "http://www.qiaotu.org/e/action/ListInfo.php?classid=2&tempid=11&orderby=diggtop&line=16"
    response = Send.send_request(Send ,url, 'text')
    indexListUrl = parseList(response)
    listUrl = []
    for urls in indexListUrl:
        res = Send.send_request(Send, urls, 'text')
        listUrls = parseDetail(res)
        listUrls.append(urls)
        for j in range(len(listUrls)):
            res2 = Send.send_request(Send, listUrls[j], 'text')
            listUrls2 = getPic(res2)
            load_dict = {
                "title": str(j),
                "pic_list": listUrls2
            }
            print("线程创建开始"+str(j))
            thread = ThreadDownLoad(load_dict, j)
            thread.start()
            #listUrl.append(listUrls)

    #print(listUrl)